<script>
    $(document).ready(function () {
        //添加
        $("#add-lg-sub").click(function () {
            var lg_user = $("#lg-user").val();
            var lg_msg = $("#lg-msg").val();
            var lg_passwd = $("#lg-passwd").val();
            var lg_key = $("#lg-key").val();
            var auth_method = $("#auth-method").val();
            $.ajax({
                type:"POST",
                async:false,
                url:"/cmdb/addlguser/",
                data:{"lg_user":lg_user,"lg_msg":lg_msg,"lg_passwd":lg_passwd,"lg_key":lg_key,"auth_method":auth_method},
                dataType : "json",
                success:function (data) {
                    $("#addLgModal").modal('hide');
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()",2100);
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            })
        });

        //获取修改内容
        $("td>a[name='edit-lg']").click(function () {
            var id = $(this).attr("lg_id");
            $.ajax({
                type: "POST",
                async: false,
                url: "/cmdb/idlguser/",
                data:{"id":id },
                dataType: "json",
                success:function (data) {
                     $("#edit-lg-user").val(data.UserName);
                    $("#edit-lg-msg").val(data.Msg);
                    $("#edit-lg-passwd").val(data.Password);
                    $("#edit-lg-key").val(data.Key);
                    $("#edit-auth-method").val(data.AuthMethod);
                    $("#edit-lg-sub").attr("lg_id",data.ID);
                    $("#editLgModal").modal("show");
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            });


        });

        //提交修改内容
        $("#edit-lg-sub").click(function () {
            var id = $(this).attr("lg_id");
            var lg_user = $("#edit-lg-user").val();
            var lg_msg = $("#edit-lg-msg").val();
            var lg_passwd = $("#edit-lg-passwd").val();
            var lg_key = $("#edit-lg-key").val();
            var auth_method = $("#edit-auth-method").val();
            $.ajax({
                type:"PUT",
                async:false,
                url:"/cmdb/editlguser/",
                data:{"lg_user":lg_user,"lg_msg":lg_msg,"lg_passwd":lg_passwd,"lg_key":lg_key,"auth_method":auth_method,"id":id},
                dataType : "json",
                success:function (data) {
                    $("#editLgModal").modal('hide');
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()",2100);
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            })
        });

        //删除角色
        $("td>a[name='del-lg']").click(function () {
            var id = $(this).attr('lg_id');
            var statu = confirm("是否确认删除!");
            if (statu==true)
            {
                $.ajax({
                    type: "post",
                    async:false,
                    url: "/cmdb/dellguser/",
                    data: {"id":id},
                    dataType : "json",
                    success: function(data) {
                        spop({
                            template:data,
                            style: 'warning',
                            autoclose: 2000
                        });
                        setTimeout("location.reload()",2100);
                    },
                    error:function (data) {
                        spop({
                            template:data.responseText,
                            style: 'error',
                            autoclose: 2000
                        });
                    }
                });
            }
        });

        //搜索角色
        $("#search-lg").bind('change',function () {
            var key = $(this).val();
            if (key != "" ) {
                var url = "/cmdb/searchlguser/" + key + "/";

            }else{
                var url = "/cmdb/lguser/";
            }
            $("#search-lg-a").attr("href", url);
        });

    })
</script>
